package com.example.personalmediaplay;

 

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ResponseCache;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;

@SuppressWarnings("deprecation")
public class menuForMain extends Activity {
	
	
	public MyVaules sp;
	public int MenuImages[]={R.drawable.menu4,R.drawable.menu1,R.drawable.menu2,R.drawable.menu3,R.drawable.menu5};
	 public TextView menuTv;
	 public GestureDetector MyGestureDetector;
	public Gallery MyGallery;
	
	public ArrayList<String> MusicList;
	public ArrayList<String> VideoList;
	public ArrayList<String> RecordList;
	
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu_main);
        //全局变量指针
        sp=(MyVaules)getApplicationContext();
       
        menuTv=(TextView)findViewById(R.id.menu_tv);
        menuTv.setBackgroundResource(R.drawable.bk);
        menuTv.setGravity(Gravity.CENTER);
       
        MyGallery=(Gallery)findViewById(R.id.gallery_menu);
        MyGallery.setAdapter(new ImageAdapter(this));
        MyGallery.setOnItemSelectedListener(new MyOnItemSelectedListener());
        MyGestureDetector =new GestureDetector(this,new MyGestureDetectorListener());
        MyGallery.setOnTouchListener(new OnTouchListener()
        {
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				return MyGestureDetector.onTouchEvent(arg1);
			}
        	
        });
        
        //启动程序第一步读取音乐列表、视频列表、录音列表
        MusicList=new  ArrayList<String>();
        VideoList=new  ArrayList<String>();
        RecordList=new  ArrayList<String>();
        Thread getlist=new Thread(new getMediaList());
        getlist.start();
         
      
     
        
	}
	
	public class getMediaList implements Runnable
	{

		@SuppressLint("SdCardPath")
		@Override
		public void run() {
			// TODO Auto-generated method stub
			//读取MusicList
			try
			{
				String listPath="/sdcard/PersonalmediaPlayer/setting/MediaList.";
				readList(listPath+"Music",MusicList);
				readList(listPath+"Video",VideoList);
				readList(listPath+"Record",RecordList);
				sp.setMusicList(MusicList);
				sp.setVideoList(VideoList);
				sp.setRecordList(RecordList);
			}
			catch(Exception e)
			{}
			
		}
		
	}
 
	@SuppressLint("SdCardPath")
	public void readList(String path,ArrayList<String> MediaList)
	{
		File file=new File(path);
		if(file.exists()==false)
			return;
		BufferedReader reader;
		try
		{
			FileInputStream fis=new FileInputStream(file);
			BufferedInputStream in=new BufferedInputStream(fis);
			in.mark(4);
			byte[]first3bytes=new byte[3];
			in.read(first3bytes);
			in.reset();
			if(first3bytes[0]==(byte)0xEF&&first3bytes[1]==(byte)0xBB&&first3bytes[2]==(byte)0xBF)
			{
				reader=new BufferedReader(new InputStreamReader(in,"UTF-8"));
			}
			else if (first3bytes[0]==(byte)0xFF&&first3bytes[1]==(byte)0xFE)
			{
				
				reader=new BufferedReader(new InputStreamReader(in,"unicode"));
			}else if(first3bytes[0]==(byte)0xFE&&first3bytes[1]==(byte)0xFF)
			{
				reader=new BufferedReader(new InputStreamReader(in,"utf-16be"));
			}else if(first3bytes[0]==(byte)0xFF&&first3bytes[1]==(byte)0xFF)
			{
				reader =new BufferedReader(new InputStreamReader(in,"utf-16le"));
			}
			else
			{
				reader=new BufferedReader(new InputStreamReader(in,"GBK"));
			}
			String str = reader.readLine();
			while(str!=null)
			{
			MediaList.add(str);
			str=reader.readLine();
			}
			reader.close();
		}
		catch(Exception e)
		{}

	}


	
	
	 private class MyGestureDetectorListener implements  OnGestureListener,OnDoubleTapListener
	 {

		@Override
		public boolean onDown(MotionEvent arg0) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
				float arg3) {
			// TODO Auto-generated method stub
			
			return false;
		}

		@Override
		public void onLongPress(MotionEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
				float arg3) {
			// TODO Auto-generated method stub
			
			return false;
		}

		@Override
		public void onShowPress(MotionEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public boolean onSingleTapUp(MotionEvent arg0) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean onDoubleTap(MotionEvent arg0) {
			// TODO Auto-generated method stub
			Intent MyIntent=new Intent();
			MyIntent.setClass(menuForMain.this,MediaPlay.class);
			Bundle MyBundle=new Bundle();
			MyBundle.putInt("name", MyGallery.getSelectedItemPosition());
			MyIntent.putExtras(MyBundle);
			startActivity(MyIntent);
			return false;
		}

		@Override
		public boolean onDoubleTapEvent(MotionEvent arg0) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean onSingleTapConfirmed(MotionEvent arg0) {
			// TODO Auto-generated method stub
			return false;
		}
		 
	 }
	public class MyOnItemSelectedListener implements OnItemSelectedListener
	{

		@SuppressLint("NewApi")
		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
		//	menuTv.setBackgroundResource(MenuImages[arg2]);
			switch(arg2)
			{
			case 1:
				menuTv.setText("音乐播放器");
				break;
			case 2:
				menuTv.setText("视频播放器");
				break;
			case 3:
				menuTv.setText("录音器");
				break;
			
			}
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	public class ImageAdapter extends BaseAdapter
	{
		private Context mContext;

		 public ImageAdapter(Context c) 
		 {
			   mContext = c;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return MenuImages.length;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}
		@TargetApi(Build.VERSION_CODES.HONEYCOMB)
		@SuppressLint("InlinedApi")
		@SuppressWarnings("deprecation")
		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// TODO Auto-generated method stub
			ImageView i = new ImageView(mContext);

			   i.setImageResource(MenuImages[arg0]);
			   i.setAdjustViewBounds(true);
			   i.setLayoutParams(new Gallery.LayoutParams(
			     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			//   i.setBackgroundResource(R.drawable.e);
			   return i;
		}
	}
	
}
